package com.das.mechanic_base.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


public class X3CameraPrograssView extends View implements View.OnClickListener {

    private Paint mPaint;

    private Context mContext;

    private float mProgress = 0;

    private float mTotalProgress = 100;

    private int mWidth,mHeight;
    private int radius;
    private DecimalFormat decimalFormat;
    private ValueAnimator valueAnimator;

    private List<Long> timeList = new ArrayList();

    public X3CameraPrograssView(Context context) {
        this(context,null);
    }

    public X3CameraPrograssView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public X3CameraPrograssView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        decimalFormat = new DecimalFormat("###.0",DecimalFormatSymbols.getInstance(Locale.CHINA));

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(X3ScreenUtils.dipToPx(4,mContext));

        this.setOnClickListener(this);

        initAnim();
    }

    private void initAnim() {
        valueAnimator = ValueAnimator.ofFloat(0,100);
        valueAnimator.setDuration(15000);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(valueAnimator1 -> {
            mProgress = (float) valueAnimator1.getAnimatedValue();
            postInvalidate();
            float animatedFraction = valueAnimator1.getAnimatedFraction();
            if (iOnShowTime != null) {
                iOnShowTime.iShowTime(decimalFormat.format(animatedFraction * 15));
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                X3CameraPrograssView.this.setVisibility(INVISIBLE);
                iOnShowTime.iShowEnd();
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        radius = getWidth() / 2;
        // 还是先画背景圆
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.parseColor("#50ffffff"));
        canvas.drawCircle(radius,radius,radius - X3ScreenUtils.dipToPx(3,mContext),mPaint);

        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        RectF rectF = new RectF(radius - X3ScreenUtils.dipToPx(10,mContext),radius - X3ScreenUtils.dipToPx(10,mContext),
                radius + X3ScreenUtils.dipToPx(10,mContext),radius + X3ScreenUtils.dipToPx(10,mContext));
        canvas.drawRoundRect(rectF, X3ScreenUtils.dipToPx(4,mContext), X3ScreenUtils.dipToPx(4,mContext),mPaint);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        RectF oval = new RectF(X3ScreenUtils.dipToPx(3,mContext), X3ScreenUtils.dipToPx(3,mContext),mWidth- X3ScreenUtils.dipToPx(3,mContext),mHeight- X3ScreenUtils.dipToPx(3,mContext));
        canvas.drawArc(oval, -90, (mProgress / mTotalProgress) * 360, false, mPaint);
    }

    public void changePrograss() {
        if (valueAnimator != null) {
            if (!X3StringUtils.isListEmpty(timeList)) {
                valueAnimator.setCurrentPlayTime(timeList.get(timeList.size() - 1));
            }
            valueAnimator.start();
        }
    }

    public ValueAnimator getValueAnimator() {
        return valueAnimator;
    }

    public void delTime () {
        if (!X3StringUtils.isListEmpty(timeList)) {
            timeList.remove(timeList.size() - 1);
        }
    }

    public void stopAnim() {
        if (valueAnimator != null) {
            long currentPlayTime = valueAnimator.getCurrentPlayTime();
            timeList.add(currentPlayTime);
            valueAnimator.cancel();
        }
    }

    public void resetPrograss() {
        this.mProgress = 0;
        invalidate();
    }

    IOnShowTime iOnShowTime;

    public void setiOnShowTime(IOnShowTime iOnShowTime) {
        this.iOnShowTime = iOnShowTime;
    }

    private long lastClickTime = 0;
    public long lastStartClickTime = 0;

    public static final int MIN_CLICK_DELAY_TIME = 500;

    @Override
    public void onClick(View v) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastClickTime < MIN_CLICK_DELAY_TIME) {
            return;
        }

        if (currentTime - lastStartClickTime < MIN_CLICK_DELAY_TIME) {
            return;
        }

        lastClickTime = currentTime;

        boolean isStop = false;
        if (valueAnimator != null && valueAnimator.isRunning()) {
            stopAnim();
            isStop = true;
            setVisibility(INVISIBLE);
        }
        if (iOnShowTime != null) {
            iOnShowTime.iShowTakeOrStop(isStop);
        }
    }

    public void stopVideo() {
        stopAnim();
        setVisibility(INVISIBLE);
        if (iOnShowTime != null) {
            iOnShowTime.iShowTakeOrStop(true);
        }
    }

    public interface IOnShowTime {
        /**
         * 用来显示拍摄时间
         * @param time
         */
        void iShowTime(String time);

        /**
         * 拍摄完成
         */
        void iShowEnd();

        /**
         * 重新开始拍摄或者暂停拍摄
         */
        void iShowTakeOrStop(boolean isStop);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int myWidthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int myWidthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int myHeightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int myHeightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        // 获取宽度
        if (myWidthSpecMode == MeasureSpec.EXACTLY) {
            // match_parent
            mWidth = myWidthSpecSize;
        } else {
            // wrap_content
            mWidth = X3ScreenUtils.dipToPx(62,mContext);;
        }

        // 获取高度
        if (myHeightSpecMode == MeasureSpec.EXACTLY) {
            mHeight = myHeightSpecSize;
        } else {
            // wrap_content
            mHeight = X3ScreenUtils.dipToPx(62,mContext);;
        }

        // 设置该view的宽高
        setMeasuredDimension(mWidth, mHeight);
    }

    public void onDestory() {
        if (valueAnimator != null) {
            valueAnimator.cancel();
        }
    }
}
